package com.gmail.blackdog1987.onedollar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期Util类
 */
public class DateUtil {
	public final static String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public final static String REGX_DATETIME = "/\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}/";
	public final static SimpleDateFormat FORMAT_DATETIME = new SimpleDateFormat(DEFAULT_DATETIME_PATTERN);
	
	public static boolean isDateTime(String dateTimeStr){
		return dateTimeStr.matches(REGX_DATETIME);
	}

	/**
	 * 返回预设Format的当前日期字符串
	 */
	public static String getToday() {
		Date today = new Date();
		return format(today);
	}

	/**
	 * 使用预设Format格式化Date成字符串
	 */
	public static String format(Date date) {
		return format(date, DEFAULT_DATETIME_PATTERN);
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 */
	public static String format(Date date, String pattern) {
		String returnValue = "";

		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}

		return (returnValue);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 */
	public static Date parse(String strDate) throws ParseException {
		return parse(strDate, DEFAULT_DATETIME_PATTERN);
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parse(String strDate, String pattern) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		return df.parse(strDate);
	}

	/**
	 * 在日期上增加数个整月
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数年
	 */
	public static Date addYear(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, n);
		return cal.getTime();
	}

	/**
	 * 在日期上加多少天
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_WEEK, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数个整分钟
	 */
	public static String addMinute(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, n);
		return format(cal.getTime());
	}

	/**
	 * 在日期上增加数个整秒
	 */
	public static String addSecond(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, n);
		return format(cal.getTime());
	}

	// public static void main(String[] args){
	// Date date=new Date();
	// System.out.println(format(date));
	// System.out.println(format(addDay(date,-10)));
	// System.out.println(format(addDay(date,10)));
	// }
}